const Service = require('egg').Service

class LoginService extends Service {

  async postLogin(body) {
    let resAdimn = await this.app.model.Admin.findOne({
      where:{adminname:body.username}
    })
    let resUser = await this.app.model.User.findOne({
      where:{username:body.username}
    })
    if(resAdimn) {
      if(body.password == resAdimn.dataValues.password) {
        const token = this.app.jwt.sign({adminname:body.username},this.app.config.jwt.secret)
        return {
          adminname:resAdimn.dataValues.adminname,
          auth:resAdimn.dataValues.auth,
          token:token
        }
      }
    }
    if(resUser) {
      if(body.password == resUser.dataValues.password) {
        return {
          username:resUser.dataValues.username,
          headImg:resUser.dataValues.headImg,
          auth:resUser.dataValues.auth,
          status:resUser.dataValues.status
        }
      }
    }
  }

}

module.exports = LoginService